/**
 * String的简单变换
 */
package com.richinfo.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hujun3
 */
public class StringUtil extends StringUtils {

	/**
	 * 将List<String>转换为需要的字符串
	 * 
	 * @param stringList
	 * @param spliter
	 * @return
	 */
	public static String getString(List<String> stringList, String spliter) {
		String msg = "";
		for (String item : stringList) {
			if (StringUtil.isValid(msg)) {
				msg = item;
			} else {
				msg += spliter + item;
			}
		}

		return msg;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isValid(String str) {
		return (str != null && str.trim().length() > 0);
	}

	public StringUtil() {
		super();
	}

	/**
	 * 对象转化为String，为空转化为空 [""]
	 * 
	 * @param obj
	 * @return
	 */
	public static String asString(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	/**
	 * 对象转化为String，为空转化为默认值 [defaultVale]
	 * 
	 * @param obj
	 * @param defaultVale
	 * @return
	 */
	public static String asString(Object obj, String defaultVale) {
		return obj == null ? defaultVale : obj.toString();
	}

	public static String HtmlEncode(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll("'", "''");
		str = str.replaceAll("\"", "&quot;");
		str = str.replaceAll(" ", "&nbsp;");
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\n", "<br>");
		return str;
	}

	public static String HtmlDecode(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		str = str.replaceAll("\n", "<br>");
		str = str.replaceAll("&gt;", ">");
		str = str.replaceAll("&lt;", "<");
		str = str.replaceAll("&nbsp;", " ");
		str = str.replaceAll("&quot;", "\"");
		str = str.replaceAll("'", "''");
		return str;
	}

	public static String sqlEncode(String strValue, boolean isLikeStatement) {
		if (strValue == null || "".equals(strValue)) {
			return "";
		}
		String rtStr = strValue;
		if (isLikeStatement) {
			rtStr = strValue.replaceAll("\\[", "[[]"); // 此句一定要在最前
			rtStr = rtStr.replaceAll("_", "[_]");
			rtStr = rtStr.replaceAll("%", "[%]");
			rtStr = rtStr.replaceAll("\\\\", "\\\\");
		}
		rtStr = rtStr.replaceAll("'", "''");
		return rtStr;
	}

	/**
	 * 取得匹配的字符串
	 * 
	 * @param input
	 * @param regex
	 * @return
	 */
	public static List<String> matchs(String input, String regex, int group) {
		Pattern pattern = Pattern.compile(regex);
		Matcher match = pattern.matcher(input);
		List<String> matches = new ArrayList<String>();
		while (match.find()) {
			matches.add(match.group(group));
		}
		return matches;
	}

	public static String toLowerCase(String in) {
		StringBuffer out = new StringBuffer();
		for (int i = 0; i < in.length(); i++) {
			char c = in.charAt(i);
			if (c >= 'A' && c <= 'Z') {
				c += 32;
			}
			out.append(c);
		}
		return out.toString();
	}

	/**
	 * 获得用户远程地址
	 */
	public static String getRemoteAddr(HttpServletRequest request) {
		String remoteAddr = request.getHeader("X-Real-IP");
		if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("X-Forwarded-For");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("Proxy-Client-IP");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("WL-Proxy-Client-IP");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("HTTP_CLIENT_IP");
		} else if (isNotBlank(remoteAddr)) {
			remoteAddr = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
	}

}
